//web-upload 工具类 @author fengshuonan 约定：上传按钮的id = 图片隐藏域id + 'BtnId' 图片预览框的id = 图片隐藏域id + 'PreId'
(function () {//
    // var PhotoUrlArray = [];
    var applicationPath = window.applicationPath === "" ? "" : window.applicationPath || "../../";
    var $IconUpload = function (pictureId) {
        var reservationsId = document.getElementById("reservationsId").value;
        var customerId = document.getElementById("customerId").value;
        var num = document.getElementById("num").value;
        var iconType = pictureId.slice(-1);
        var cri = customerId + "," + reservationsId + "," + iconType + "," + num;
        this.pictureId = pictureId;
        this.uploadBtnId = pictureId + "BtnId";
        this.uploadPreId = pictureId + "PreId";
        this.uploadUrl = applicationPath + '/doctorLooked/uploadIcons/' + cri;
        this.picWidth = 280;
        this.picHeight = 160;
    };
    $IconUpload.prototype = {
        initIcon: function () {//初始化webUploader
            var uploader = this.create();
            this.bindEvent(uploader);
            return uploader;
        },
        create: function () {//创建webuploader对象
            var webUploader = WebUploader.create({
                auto: true,
                pick: {
                    id: '#' + this.uploadBtnId,
                    multiple: false// 是否能上传多张
                },
                accept: {
                    title: 'Images',
                    extensions: 'jpg',
                    mimeTypes: 'image/jpg'
                },
                swf: Feng.ctxPath + '${ctxPath}/static/css/plugins/webuploader/Uploader.swf',
                disableGlobalDnd: true,
                duplicate: true,
                server: this.uploadUrl
            });
            return webUploader;
        },
        bindEvent: function (bindObj) {//绑定事件
            var me = this;
            bindObj.on('fileQueued', function (file) {//当有文件添加进来的时候
                bindObj.makeThumb( file, function( error, ret ) {
                    if(file._info.height > file._info.width){
                        layer.msg("图片尺寸高度不能大于宽度！");
                        bindObj.reset();
                        return false;
                    }
                });
            });
            bindObj.on('uploadProgress', function (file, percentage) {//文件上传过程中创建进度条实时显示
                var $li = $('#' + file.id),
                    $percent = $li.find('.progress span');
                if (!$percent.length) {// 避免重复创建
                    $percent = $('<p class="progress"><span></span></p>')
                        .appendTo($li)
                        .find('span');
                }
                $percent.css('width', percentage * 100 + '%');
            });
            bindObj.on('uploadSuccess', function (file, response) {//文件上传成功，给item添加成功class, 用样式标记上传成功。
                $('#' + file.id).addClass('upload-state-done');
                $("#" + me.uploadBtnId).css({
                    'pointer-events': 'none',
                    'cursor': 'not-allowed',
                    'background': '#DDDDDD'
                });
                // PhotoUrlArray.push(new PhotoUrl(response.id, response.filePath)); //将上传的url保存到数组
            });
            bindObj.on('uploadComplete', function (file) {//完成上传完了，成功或者失败，先删除进度条。
                $('#' + file.id).find('.progress').remove();
            });
            bindObj.on("uploadFinished", function () {//所有文件上传完毕
                var reservationsId = $("#reservationsId").val();
                var num = $("#num").val();
                $.ajax({
                    url: Feng.ctxPath + "/doctorLooked/getIntelligentImgcancel1",
                    type: "post",
                    data: {
                        'reservationsId': reservationsId,
                        'num': num
                    },
                    success: function (data) {
                        $("#avatar0PreId").html("");
                        $("#avatar1PreId").html("");
                        $("#avatar2PreId").html("");
                        var imgBox0 = "";
                        var imgBox1 = "";
                        var imgBox2 = "";
                        for (var i = 0; i < data.length; i++) {
                            if (data[i].iconName == "术前") {
                                imgBox0 = $('<div id="' + data[i].id + '" class="cp_img" style="width: 100%;height:120px;padding: 8px 9px;border: 1px solid #DDDDDD;position: relative">' +
                                    '<img class="imagePath" id="' + data[i].iconName + '" src="' + data[i].imageUrl + "?temp=" + Math.random() + '" alt="" style="width: 100%;height: 100px;">' +
                                    '<div class="cp_img_delete0" style="text-align: center;position: absolute;width: 92%;bottom: -28px;color: red">删除' + data[i].iconName + '</div></div>');
                            } else if (data[i].iconName == "术中") {
                                imgBox1 = $('<div id="' + data[i].id + '" class="cp_img" style="width: 100%;height:120px;padding: 8px 9px;border: 1px solid #DDDDDD;position: relative">' +
                                    '<img class="imagePath" id="' + data[i].iconName + '" src="' + data[i].imageUrl + "?temp=" + Math.random() + '" alt="" style="width: 100%;height: 100px;">' +
                                    '<div class="cp_img_delete1" style="text-align: center;position: absolute;width: 92%;bottom: -28px;color: red">删除' + data[i].iconName + '</div></div>');
                            } else if (data[i].iconName == "术后") {
                                imgBox2 = $('<div id="' + data[i].id + '" class="cp_img" style="width: 100%;height:120px;padding: 8px 9px;border: 1px solid #DDDDDD;position: relative">' +
                                    '<img class="imagePath" id="' + data[i].iconName + '" src="' + data[i].imageUrl + "?temp=" + Math.random() + '" alt="" style="width: 100%;height: 100px;">' +
                                    '<div class="cp_img_delete2" style="text-align: center;position: absolute;width: 92%;bottom: -28px;color: red">删除' + data[i].iconName + '</div></div>');
                            }
                        }
                        $("#avatar0PreId").append(imgBox0);
                        $("#avatar1PreId").append(imgBox1);
                        $("#avatar2PreId").append(imgBox2);
                    },
                    error: function (data) {
                        alert('没有权限，请联系管理员!');
                    }
                });
            });
            bindObj.on("error", function (type) {
                if (type == "Q_TYPE_DENIED") {
                    layer.msg("请上传JPG格式文件");
                }
            });
            $(document).on('mouseover', '.cp_img', function () {
                $(this).children(".cp_img_jian").css('display', 'block');
            });
            $(document).on('mouseout', '.cp_img', function () {
                $(this).children(".cp_img_jian").css('display', 'none');
            });
        }
    };
    window.$IconUpload = $IconUpload;
}());
